var express = require('express');
var router = express.Router();

const { showBooksList, showSubject, showCategory, showBooksDetails, showHotbooks, getBooksByCategory, showEditorRecommend, getSections, showHotbooksBySection, searchBooks, getRandomBooks, getAdminBooksList, getAdminBookDetail, updateBookStatus, updateBook, addBook, deleteBook } = require('../controls/booksCtrl.js')
const { doLogin, doReg, getUserList, updateUserStatus, updateUser, deleteUser, adminLogin } = require('../controls/userCtrl.js')
const { doAddCart, showCart, doUpdateCartNum, doDelCart } = require('../controls/cartCtrl.js')
const { doAddOrder, showOrders, deleteOrder } = require('../controls/orderCtrl.js')
const { getStatistics, getHotBooks, getRecentActivities } = require('../controls/dashboardCtrl.js')
const upload = require('../util/upload'); // 引入上传模块



const multer = require('multer')
const path = require('path')
// 配置上传选项
let storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, '../public/head'))
  },
  filename: (req, file, cb) => {
    cb(null, Date.now() + path.extname(file.originalname))
  }
})
// 创建multer实例对象
// const upload = multer({ storage })

// 获取书籍信息
router.get('/booksList', showBooksList)

// 搜索书籍
router.get('/searchBooks', searchBooks)

// 获取随机书籍
router.get('/randomBooks', getRandomBooks)

// 获取主题信息/booksSubject
router.get('/booksSubject', showSubject)

// 获取所有书籍分类信息
router.get('/category', showCategory)

// 获取书籍详情（包含对应的评论）
router.get('/booksDetails/:id', showBooksDetails)

// 获取分类下的所有书籍
router.get('/booksByCategory/:category_id', getBooksByCategory)

// 展示首页推荐版块
router.get('/hotBook',showHotbooks)

// 主编推荐接口
router.get('/editorRecommend', showEditorRecommend)

// 栏目接口
router.get('/sections', getSections)

// 栏目下的推荐图书接口
router.get('/hotBookBySection', showHotbooksBySection)

// 用户登录
router.post('/login', doLogin)

// 管理员登录
router.post('/admin/login', adminLogin)

// 用户注册
router.post('/register', upload.single('head_img'), doReg)

// 加入购物车
router.post('/addCart', doAddCart)

// 展示购物车
router.get('/shopcartList', showCart)

// 修改购物车数量
router.post('/updateCartNum', doUpdateCartNum)

// 删除购物车数据
router.get('/delCart', doDelCart)

// 提交订单
router.post('/addOrder', doAddOrder)

// 展示订单
router.get('/orderList', showOrders)

// 删除订单
router.delete('/orderList/:id', deleteOrder)

// 获取用户列表
router.get('/admin/users', getUserList)

// 更新用户状态
router.put('/admin/users/status', updateUserStatus)

// 更新用户信息
router.put('/admin/users', upload.single('head_img'), updateUser)

// 删除用户
router.delete('/admin/users/:id', deleteUser)

// 仪表盘相关路由
router.get('/admin/dashboard/statistics', getStatistics)
router.get('/admin/dashboard/hotBooks', getHotBooks)
router.get('/admin/dashboard/activities', getRecentActivities)

// 图书管理相关路由
router.get('/admin/books', getAdminBooksList)
router.get('/admin/books/:id', getAdminBookDetail)
router.put('/admin/books/status', updateBookStatus)
router.put('/admin/books', updateBook)
router.post('/admin/books', addBook)
router.delete('/admin/books/:id', deleteBook)

module.exports = router;
